package fm.dongman.animefm.view.activity;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import fm.dongman.animefm.R;
import fm.dongman.animefm.view.fragment.WebViewFragment;
import fm.dongman.animefm.view.fragment.WebViewVideoFragment;

/**
 * @author Alex 2017.10.18
 * @description 用于打开http页面的WebView Activity
 */
public class WebViewActivity extends BaseActivity {

    private static final String ARG_URL = "http_url";
    private static final String ARG_READ_MODE = "launch_mode";

    private String mUrlAdd;
    private boolean mReadMode = false;    //启动模式
    private WebViewFragment mWebViewFragment;

    /**
     * 返回此Activity启动所需的Intent
     *
     * @param context 上下文
     * @param url     http网络地址
     * @return 启动Activity的Intent
     */
    public static Intent getIntent(Context context, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(ARG_URL, url);
        return intent;
    }

    /**
     * 返回此Activity启动所需的Intent
     *
     * @param context 上下文
     * @param url   http网络地址
     * @param readMode  是否为阅读模式（仅保留标题，隐藏刷新、复制链接等功能）
     * @return  启动Activity的Intent
     */
    public static Intent getIntent(Context context, String url, boolean readMode) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(ARG_URL, url);
        intent.putExtra(ARG_READ_MODE, readMode);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single);
        mReadMode = getIntent().getBooleanExtra(ARG_READ_MODE, false);
        mUrlAdd = getIntent().getStringExtra(ARG_URL);
        mWebViewFragment = WebViewFragment.newInstance(mUrlAdd, mReadMode);
//        mWebViewFragment = new WebViewVideoFragment();
//        mWebViewFragment.setUrl(mUrlAdd);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
        getFragmentManager().beginTransaction()
                .replace(R.id.mSingleContent, addFragment())
                .commit();
    }

    protected Fragment addFragment() {
        return mWebViewFragment;
    }

    @Override
    public void onBackPressed() {
        if (mWebViewFragment.isWebViewCanGoBack()) {
            mWebViewFragment.webViewGoBack();
            return;
        }
        super.onBackPressed();
    }

}
